#include "RessourceManager.hpp"

RessourceManager::RessourceManager()
{
}

RessourceManager::~RessourceManager()
{
}

ImageRessource* RessourceManager::AddImage(const std::string nameID, const std::string filename)
{
	ImageRessource* imagePtr = NULL;

	std::map<const std::string, ImageRessource*>::iterator iter;
	iter = m_images.find(nameID);

	if(iter == m_images.end())
	{
		imagePtr = new ImageRessource(filename);
		m_images.insert(std::pair<const std::string, ImageRessource*>(nameID, imagePtr));

		imagePtr->LoadRessource();
	}
	else
		imagePtr = iter->second;


	return imagePtr;

}

FontRessource* RessourceManager::AddFont(const std::string nameID, const std::string filename)
{
	FontRessource* fontPtr = NULL;

	std::map<const std::string, FontRessource*>::iterator iter;
	iter = m_fonts.find(nameID);

	if(iter == m_fonts.end())
	{
		fontPtr = new FontRessource(filename);
		m_fonts.insert(std::pair<const std::string, FontRessource*>(nameID, fontPtr));

		fontPtr->LoadRessource();
	}
	else
		fontPtr = iter->second;

	return fontPtr;

}

MusicRessource* RessourceManager::AddMusic(const std::string nameID, const std::string filename)
{
	MusicRessource* musicPtr = NULL;

	std::map<const std::string, MusicRessource*>::iterator iter;
	iter = m_music.find(nameID);

	if(iter == m_music.end())
	{
		musicPtr = new MusicRessource(filename);
		m_music.insert(std::pair<const std::string, MusicRessource*>(nameID, musicPtr));

		musicPtr->LoadRessource();
	}
	else
		musicPtr = iter->second;

	return musicPtr;

}

SoundRessource* RessourceManager::AddSound(const std::string nameID, const std::string filename)
{
	SoundRessource* soundPtr = NULL;

	std::map<const std::string, SoundRessource*>::iterator iter;
	iter = m_sounds.find(nameID);

	if(iter == m_sounds.end())
	{
		soundPtr = new SoundRessource(filename);
		m_sounds.insert(std::pair<const std::string, SoundRessource*>(nameID, soundPtr));

		soundPtr->LoadRessource();
	}
	else
		soundPtr = iter->second;

	return soundPtr;
}

////////


ImageRessource* RessourceManager::Image(const std::string nameID)
{
	ImageRessource* imagePtr = NULL;

	std::map<const std::string, ImageRessource*>::iterator iter;
	iter = m_images.find(nameID);

	if(iter != m_images.end())
		imagePtr = iter->second;

	return imagePtr;

}

FontRessource* RessourceManager::Font(const std::string nameID)
{
	FontRessource* fontPtr = NULL;

	std::map<const std::string, FontRessource*>::iterator iter;
	iter = m_fonts.find(nameID);

	if(iter != m_fonts.end())
		fontPtr = iter->second;

	return fontPtr;
}

MusicRessource* RessourceManager::Music(const std::string nameID)
{
	MusicRessource* musicPtr = NULL;

	std::map<const std::string, MusicRessource*>::iterator iter;
	iter = m_music.find(nameID);

	if(iter != m_music.end())
		musicPtr = iter->second;

	return musicPtr;
}

SoundRessource* RessourceManager::Sound(const std::string nameID)
{
	SoundRessource* soundPtr = NULL;

	std::map<const std::string, SoundRessource*>::iterator iter;
	iter = m_sounds.find(nameID);

	if(iter != m_sounds.end())
		soundPtr = iter->second;

	return soundPtr;
}

////

void RessourceManager::UnloadImage(const std::string nameID)
{
	std::map<const std::string, ImageRessource*>::iterator iter;
	iter = m_images.find(nameID);

	if(iter != m_images.end())
	{
		iter->second->UnloadRessource();

		// Remove Resource (std::map)
		m_images.erase(iter);

	}
}

void RessourceManager::UnloadFont(const std::string nameID)
{
	std::map<const std::string, FontRessource*>::iterator iter;
	iter = m_fonts.find(nameID);

	if(iter != m_fonts.end())
	{
		iter->second->UnloadRessource();

		// Remove Ressource Font ( std::map )
		m_fonts.erase(iter);

	}
}


void RessourceManager::UnloadMusic(const std::string nameID)
{
	std::map<const std::string, MusicRessource*>::iterator iter;
	iter = m_music.find(nameID);

	if(iter != m_music.end())
	{
		iter->second->UnloadRessource();

		// Remove Resource Music ( std::map )
		m_music.erase(iter);
	}
}

void RessourceManager::UnloadSound(const std::string nameID)
{
	std::map<const std::string, SoundRessource*>::iterator iter;
	iter = m_sounds.find(nameID);

	if(iter != m_sounds.end())
	{
		iter->second->UnloadRessource();

		// Remove Resource Sound ( std::map )
		m_sounds.erase(iter);
	}
}

///// Delete

void RessourceManager::DeleteImages()
{
	std::map<const std::string, ImageRessource*>::iterator iter;
	iter = m_images.begin();

	while(iter != m_images.end())
	{
		ImageRessource* imagePtr = iter->second;

		m_images.erase(iter++);
		delete imagePtr;

	}
}

void RessourceManager::DeleteFonts()
{
	std::map<const std::string, FontRessource*>::iterator iter;
	iter = m_fonts.begin();

	while(iter != m_fonts.end())
	{
		FontRessource* fontPtr = iter->second;

		m_fonts.erase(iter++);
		delete fontPtr;
	}
}


void RessourceManager::DeleteMusic()
{
	std::map<const std::string, MusicRessource*>::iterator iter;
	iter = m_music.begin();

	while(iter != m_music.end())
	{
		MusicRessource* musicPtr = iter->second;

		m_music.erase(iter++);
		delete musicPtr;
	}
}

void RessourceManager::DeleteSounds()
{
	std::map<const std::string, SoundRessource*>::iterator iter;
	iter = m_sounds.begin();

	while(iter != m_sounds.end())
	{
		SoundRessource* soundPtr = iter->second;

		m_sounds.erase(iter++);
		delete soundPtr;
	}
}


const std::string RessourceManager::ImageNameID(const std::string filename)
{
	std::map<const std::string, ImageRessource*>::iterator iter;

	for(iter = m_images.begin(); iter != m_images.end(); iter++)
		if(iter->second->Filename() == filename)
			return iter->first;

	return "";

}

const std::string RessourceManager::FontNameID(const std::string filename)
{
	std::map<const std::string, FontRessource*>::iterator iter;

	for(iter = m_fonts.begin(); iter != m_fonts.end(); iter++)
		if(iter->second->Filename() == filename)
			return iter->first;

	return "";
}


const std::string RessourceManager::MusicNameID(const std::string filename)
{
	std::map<const std::string, MusicRessource*>::iterator iter;

	for(iter = m_music.begin(); iter != m_music.end(); iter++)
		if(iter->second->Filename() == filename)
			return iter->first;

	return "";
}

const std::string RessourceManager::SoundNameID(const std::string filename)
{
	std::map<const std::string, SoundRessource*>::iterator iter;

	for(iter = m_sounds.begin(); iter != m_sounds.end(); iter++)
		if(iter->second->Filename() == filename)
			return iter->first;

	return "";

}
